package com.xing.algorithm.linkedlist;

class ReverseList {

    public static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }

        ListNode newHead = null;
        ListNode cur = null;

        while(head != null) {
            cur = head.next;

            head.next = newHead;
            newHead = head;

            head = cur;
        }

        return newHead;

    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode();
        head1.val = 1;

        ListNode head2 = new ListNode();
        head2.val = 2;

        head1.next = head2;

        ListNode newHead = reverseList(head1);

        StringBuilder sb = new StringBuilder();
        while(newHead != null) {
            sb.append(newHead.val).append(">");
            newHead = newHead.next;
        }

        System.out.println(sb.toString());
    }

    static class ListNode {
        public int val;
        public ListNode next;

    }
}

